#ifndef SANGAME_HISTORY_LAYOUT_H_
#define SANGAME_HISTORY_LAYOUT_H_

#include "base/message_loop.h"
#include "history/indicator_message.h"

namespace sangame {
	namespace history {

class Layout : public base::MessageLoop, public CLayerBufferDraw<CHistoryLayout>, 
	public CXMLStringStatus<CHistoryLayout> {
    enum Setting {
        TITLE_HEIGHT = 20,
        MIN_SHOW_COUNT = 9,
        DEFAULT_SHOW_COUNT = 100,
        XAXIS_HEIGHT = 20,
        MIN_X_UNIT_WIDTH = 40,
        PREFETCH_SIZE = 120,
        LEFT_YAXIS_WIDTH = 55,
        RIGHT_YAXIS_WIDTH = 55,
		START_STATUS_ID = 0,
    };

public:
    Layout();
    ~Layout();

    #pragma region SG Message

public:
	bool OnCreate(base::Handle* message_ptr);
	bool OnSetRect(base::Rect* message_ptr);
	bool OnDraw(base::CanvasMessage* message_ptr);
	bool OnKeyDown(base::KeyMessage* message_ptr);
	bool OnLButtonDown(base::MouseMessage* message_ptr);
	bool OnLButtonDblClk(base::MouseMessage* message_ptr);
	bool OnRButtonUp(base::MouseMessage* message_ptr);
	bool OnMouseMove(base::MouseMessage* message_ptr);
	bool OnMouseWheel(base::MouseMessage* message_ptr);
	bool OnActivate(bool* message_ptr);
	bool OnMessage(int message_type, void* message_ptr);

private:
	bool OnInitContext(ContextMessage* message_ptr);
	bool OnChangeGoods(GoodsChangeMessage* message_ptr);
	bool OnLoadData();
	bool DispatchMessage(int message_type, void* message_ptr);

    #pragma endregion

    #pragma region Draw

public:
    void OnDrawBack(base::CanvasMessage* message_ptr);
    void OnDrawFore(base::CanvasMessage* message_ptr);

private:
	void DrawFrame(base::CanvasMessage* message_ptr);
	void DrawTitle(base::CanvasMessage* message_ptr);
    void DrawXAxis(base::CanvasMessage* message_ptr);
    void DrawCross(base::CanvasMessage* message_ptr);
    void DrawMousePos(base::CanvasMessage* message_ptr);
    void DrawTracker(base::CanvasMessage* message_ptr);

    base::Rect view_rect_;
    base::Rect title_rect_;
    base::Rect x_coord_rect_;

    #pragma endregion

    #pragma region XMLString Status

public:
    BOOL LoadXml(std::string& xml);
    BOOL SaveXml(std::string& xml);
	int GetStatusID();
    void OnStatusUpdate();

    struct Status {
		int m_nViewCount;
		std::vector<std::string> m_vtIndicatorName;
    } m_tStatus;

    #pragma endregion

    #pragma region PZ Toolbar

    #pragma endregion

    #pragma region SG Data

public:
	typedef std::vector<uint64_t> XValueList;

	void OnDataUpdate();
	uint64_t GetValidBeginTime();
	std::shared_ptr<CHistoryData> GetData();
	XValueList& GetXValueList();

private:
	BOOL CheckData();
	void SyncData();

	XValueList m_vtX;
	std::shared_ptr<CHistoryData> m_pMainData;
	int m_nValidBeginPos;
	int m_nValidEndPos;
	int m_nShowCount;
	uint64_t m_nValidBeginTime;

#pragma endregion

#pragma region Accessor

public:
	void OnSelectChange();

private:
	bool IsMain(std::shared_ptr<View> view_ptr);
	bool IsSelected(std::shared_ptr<View> view_ptr);
	void UpdateMainAccessor();
	void UpdateSelectedAccessor();

	std::vector<std::shared_ptr<View>> view_ptr_list_;
	std::weak_ptr<View> weak_main_view_ptr;
	std::weak_ptr<View> weak_selected_view_ptr;

#pragma endregion

private:
    CString FormatDateString( int nType, uint64_t nDate, wchar_t* szFormat = nullptr, bool bWeek = true);
    COLORREF GetColor(double v1, double v2);
	void UpdateSelect();
	void MoveLeft();
	void MoveRight();
	void MoveSpanLeft();
	void MoveSpanRight();
	void UpdateCrossIndicator();
	SGHandle GetMainView();
	void OnAddGoods();
	void OnAddArbitrage();

    CPoint m_ptCross;
    BOOL m_bCross;
    BOOL m_bTrackerLeft;
    BOOL m_bReady;
    BOOL m_bYAxisPosLeft;
	int m_nCrossDataIndicator;

};

}
}

#endif // SANGAME_HISTORY_LAYOUT_H_